package com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hyphenate.easeui.utils.map.JSONUtils;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.kongzue.dialog.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialog.util.BaseDialog;
import com.kongzue.dialog.v3.MessageDialog;
import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.SpApplication;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.CollectionContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.presenter.CollectionPresenter;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter.DHomeAdapter;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;
import com.ninetaildemonfox.zdl.socialproject.utils.dialog.DialogShow;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author NineTailDemonFox
 * @date 2019/7/27 13:34
 * 功能描述： 我的收藏
 * 联系方式：1037438704@qq.com
 */
public class CollectionActivity extends BaseActivity implements CollectionContract {
    @BindView(R.id.image_left_finish)
    ImageView imageleftFinish;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.text_right)
    TextView textRight;
    @BindView(R.id.recyclerView)
    EasyRecyclerView recyclerView;
    private CollectionPresenter presenter;
    private int page = 1;
    private DHomeAdapter dHomeAdapter;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_collection;
    }

    @Override
    protected void onInitialization(Bundle bundle) {
        presenter = new CollectionPresenter(me, this);
        imageleftFinish.setVisibility(View.VISIBLE);
        textCenter.setText("我的收藏");
        textRight.setText("全部取消");
        textRight.setVisibility(View.VISIBLE);

        recyclerView.setLayoutManager(new LinearLayoutManager(me));
        recyclerView.setRefreshingColor(Color.rgb(255, 99, 71), Color.rgb(255, 99, 71), Color.rgb(255, 99, 71));
        dHomeAdapter = new DHomeAdapter(me);
//        dHomeAdapter.setNoMore(R.layout.view_nomore);
//        dHomeAdapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
//            @Override
//            public void onErrorShow() {
//                dHomeAdapter.resumeMore();
//            }
//
//            @Override
//            public void onErrorClick() {
//                dHomeAdapter.resumeMore();
//            }
//        });
        recyclerView.setAdapter(dHomeAdapter);


        dHomeAdapter.setmOnClick(new DHomeAdapter.AdpItemOnClick() {
            @Override
            public void onClick(int position, Map<String, String> thisdata, int type) {
                String member_id = thisdata.get("id");
                if (type == 0) {
                    presenter.addMylike(token, member_id);
                    return;
                } else {
                    //                             是否vip 1否 2是           1未认证 2 已认证
                    if (getSex.equals("1")) {
                        if (getVip.equals("1")) {
                            HttpRequest.POST(me, "Index/seeCount"
                                    , new Parameter()
                                            .add("token", token)
                                            .add("cover_member_id", member_id)
                                    , new ResponseListener() {
                                        @Override
                                        public void onResponse(String response, Exception error) {
                                            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                                            Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                                            int count = Integer.valueOf(data.get("count"));
                                            if (data.get("look").equals("1")) {
                                                DetailInterfaceActivity.start(me, member_id);
                                                return;
                                            }
                                            if (count <= 3 && count > 0) {
                                                //立即开通
                                                MessageDialog.show(me, "剩余机会" + data.get("count"), "非会员每天只能查看" + data.get("total") + "位女士", "升级会员", "继续查看")
                                                        .setOnOkButtonClickListener(new OnDialogButtonClickListener() {
                                                            @Override
                                                            public boolean onClick(BaseDialog baseDialog, View v) {
                                                                VipActivity.start(me, "1", 1);
                                                                return false;
                                                            }
                                                        })
                                                        .setOnCancelButtonClickListener(new OnDialogButtonClickListener() {
                                                            @Override
                                                            public boolean onClick(BaseDialog baseDialog, View v) {
                                                                DetailInterfaceActivity.start(me, member_id);
                                                                return false;
                                                            }
                                                        })
                                                ;
                                            } else if (count == 0) {
                                                ToastUtil.show("您没有次数了");
                                            } else {
                                                DetailInterfaceActivity.start(me, member_id);
                                            }
                                        }
                                    });
                        } else {
                            //会员
                            DetailInterfaceActivity.start(me, member_id);
                        }
                    } else {
                        if (getIdentification.equals("1")) {
                            HttpRequest.POST(me, "Index/seeCount"
                                    , new Parameter()
                                            .add("token", token)
                                            .add("cover_member_id", member_id)
                                    , new ResponseListener() {
                                        @Override
                                        public void onResponse(String response, Exception error) {
                                            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                                            Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                                            int count = Integer.valueOf(data.get("count"));
                                            if (data.get("look").equals("1")) {
                                                DetailInterfaceActivity.start(me, member_id, "1");
                                                return;
                                            }

                                            if (count <= 3 && count > 0) {
                                                //立即开通
                                                MessageDialog.show(me, "剩余机会" + data.get("count"), "未认证每天只能查看" + data.get("total") + "位男士", "去认证", "继续查看")
                                                        .setOnOkButtonClickListener(new OnDialogButtonClickListener() {
                                                            @Override
                                                            public boolean onClick(BaseDialog baseDialog, View v) {
                                                                //去认证
                                                                Intent intent = new Intent(me, AuthenticationCenterActivity.class);
                                                                startActivity(intent);
                                                                return false;
                                                            }
                                                        })
                                                        .setOnCancelButtonClickListener(new OnDialogButtonClickListener() {
                                                            @Override
                                                            public boolean onClick(BaseDialog baseDialog, View v) {
                                                                DetailInterfaceActivity.start(me, member_id);
                                                                return false;
                                                            }
                                                        })
                                                ;
                                            } else if (count == 0) {
                                                ToastUtil.show("您没有次数了请先认证");
                                                return;
                                            } else {
                                                DetailInterfaceActivity.start(me, member_id);
                                            }
                                        }
                                    });
                        } else {
                            DetailInterfaceActivity.start(me, member_id);
                        }
                    }
                }
            }
        });
    }

    private void http() {
        presenter.getLikeList(token, page, SpApplication.LONGITUDE, SpApplication.LATITUDE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        http();
    }

    @OnClick({R.id.image_left_finish, R.id.text_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_left_finish:
                finish();
                break;
            case R.id.text_right:
                DialogShow.start(me, "8");
                break;
            default:
        }
    }

    @Override
    public void collectSuccess(List<Map<String, String>> data) {
        if (page == 1) {
            dHomeAdapter.clear();
            dHomeAdapter.addAll(data);
        }


    }

    @Override
    public void Failed(String msg) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void memberInfor() {
        page = 1;
        http();
    }
}
